package com.wlw.eye.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Typeface
import android.os.Parcelable
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.wlw.eye.R
import com.wlw.eye.mvp.model.bean.ItemListBean
import com.wlw.eye.mvp.model.bean.VideoBean
import com.wlw.eye.ui.VideoDetailActivity
import com.wlw.eye.utils.ImageLoadUtils
import com.wlw.eye.utils.ObjectSaveUtils
import com.wlw.eye.utils.SPUtils
import kotlinx.android.synthetic.main.item_home.view.*
import kotlin.math.log

class HomeAdapter(list: MutableList<ItemListBean>) : RecyclerView.Adapter<HomeAdapter.HomeViewHolder>() {
    private var list: MutableList<ItemListBean>? = null

    init {
        this.list = list
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeViewHolder {
        return HomeViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_home, parent, false))
    }

    override fun getItemCount(): Int = list?.size ?: 0
    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: HomeViewHolder, position: Int) {
        val itemView = holder.itemView
        val bean = list!![position]
        val title = bean.data?.title
        var category = bean.data?.category
        val minute = bean.data?.duration?.div(60)
        val second = bean.data?.duration?.minus((minute?.times(60)) as Long)

        var realMinute: String = if (minute!! < 10) "0$minute" else minute.toString()
        var realSecond: String = if (second!! < 10) "0$second" else second.toString()

        var photo = bean.data?.cover?.feed
        var author = bean.data?.author
        ImageLoadUtils.display(itemView.context, itemView.iv_photo, photo.toString())
        itemView.tv_title.text = title
        itemView.tv_detail.text = "发布于$category/$realMinute/$realSecond"
        if (author != null) {
            ImageLoadUtils.display(itemView.context, itemView.iv_user, author.icon)
        } else {
            itemView.iv_user.visibility = View.GONE
        }
        itemView.setOnClickListener {
            val intent = Intent(itemView.context, VideoDetailActivity::class.java)
            val desc = bean.data?.description
            val duration = bean.data?.duration
            var playUrl = bean.data?.playUrl
            val blurred = bean.data?.cover?.blurred
            val collect = bean.data?.consumption?.collectionCount
            val share = bean.data?.consumption?.shareCount
            val reply = bean.data?.consumption?.replyCount
            val time = System.currentTimeMillis()
            val videoBean = VideoBean(photo, title, desc, duration, playUrl, category, blurred, collect, share, reply, time)
            val url = SPUtils.getInstance(itemView.context, "beans").getString(playUrl!!)
            Log.e("bean", url)
            if (url.isEmpty()) {
                var count = SPUtils.getInstance(itemView.context, "beans").getInt("count", -1)
                count = if (count != -1) count.inc() else 1
                Log.e("bean", count.toString())
                SPUtils.getInstance(itemView.context, "beans").put("count", count)
                SPUtils.getInstance(itemView.context, "beans").put(playUrl, playUrl)
                Log.e("bean", playUrl)
                ObjectSaveUtils.saveObject(itemView.context, "bean$count", videoBean)
                Log.e("bean", "bean$count")
            }
            intent.putExtra("data", videoBean as Parcelable)
            itemView.context.let { context -> context.startActivity(intent) }
        }
    }

    public class HomeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}

